Stored Procedures [dbo].[asi_NextSequenceValueTest]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@sequenceNamenvarchar(30)60
@userKeyuniqueidentifier16
@systemEntityKeyuniqueidentifier16
@incrementint4
@startValueint4
SQL Script
CREATE proc [dbo].[asi_NextSequenceValueTest]
  @sequenceName nvarchar(30),
  @userKey uniqueidentifier,
  @systemEntityKey uniqueidentifier = '00000000-0000-0000-0000-000000000000',
  @increment int = 1,
  @startValue int = 0 as

declare @lastValue int

set nocount on

exec asi_NextSequenceValueSub @sequenceName, @userKey, @systemEntityKey, @increment, @startValue, @lastValue output

-- Counter Filtering / 666 handler
if exists (select 1 from SystemConfig where ParameterName = 'EnableCounterFiltering' and ParameterValue = 'True')
begin
   declare @checkValue int
   declare @checkValueStr varchar(11)
      
   set @checkValue = @lastValue - (@increment - 1)
   while @checkValue <= @lastValue
   begin
      set @checkValueStr = convert (varchar(11), @checkValue)
      while charindex('666', @checkValueStr) > 0
      begin
         exec asi_NextSequenceValueSub @sequenceName, @userKey, @systemEntityKey, 1, @startValue, @lastValue output
         set @checkValue = @checkValue + 1
         set @checkValueStr = convert (varchar(11), @checkValue)
      end         
      set @checkValue = @checkValue + 1
   end
end

set nocount off

--Return the last value of the assigned counter range
select @lastValue CurrentValue

GO
Uses